package org.webscale.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FileUtil {

	public static List<File> listSubDirs(File parent) {
		ArrayList<File> fileList = new ArrayList<File>();

		File[] files = parent.listFiles(new FileFilter() {
		
			public boolean accept(File pathname) {
				if (pathname.isDirectory())
					return true;
				else
					return false;
			}
		});

		fileList.addAll(Arrays.asList(files));
		for (File file : files) {
			fileList.addAll(listSubDirs(file));
		}
		return fileList;
	}

	public static boolean isValidImageFile(String fileName){
		fileName = fileName.toLowerCase();
		if(fileName.endsWith(".jpg") ||
		   fileName.endsWith(".jpeg") ||
		   fileName.endsWith(".gif") ||
		   fileName.endsWith(".png")
				){
			return true;
		}else{
			return false;
		}
	}
	
	public static boolean isValidMp3File(String fileNameOrPath){
		fileNameOrPath = fileNameOrPath.toLowerCase();
		if(fileNameOrPath.endsWith(".mp3")){
			return true;
		}else{
			return false;
		}
	}
	
	public static boolean isDirectoryInPathForFile(File dir, File path) {
		String dirPath = dir.getAbsolutePath();
		String filePath = path.getAbsolutePath();
		int i = filePath.indexOf(dirPath);
		if (filePath.indexOf(dirPath) >= 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public static String extractFileNameFromFilePath(String filePath, String endsWithString){
		filePath = filePath.toLowerCase();
		if(!filePath.endsWith(endsWithString.toLowerCase())){
			return null;
		}
		int i = filePath.lastIndexOf(File.separator);
		String filename = filePath.substring(i);
		filename = filename.substring(1, filename.indexOf(endsWithString.toLowerCase()));
		return filename;
	}
	
	
	public static void copy(File fromFile, File toFile , boolean overwrite) throws IOException{
		String fromFileName = fromFile.getAbsolutePath();
		String toFileName = toFile.getAbsolutePath();

		if (!fromFile.exists())
			throw new IOException("FileCopy: " + "no such source file: "
					+ fromFileName);
		if (!fromFile.isFile())
			throw new IOException("FileCopy: " + "can't copy directory: "
					+ fromFileName);
		if (!fromFile.canRead())
			throw new IOException("FileCopy: " + "source file is unreadable: "
					+ fromFileName);

		if (toFile.isDirectory())
			toFile = new File(toFile, fromFile.getName());

		if (toFile.exists() && !overwrite) {
			if (!toFile.canWrite())
			{
				throw new IOException("FileCopy: "
						+ "destination file is unwriteable: " + toFileName);
			}
			
			long diff= toFile.length() - fromFile.length();
			if(diff < 10){
				System.out.println("Skipping FILE " + toFileName + " As it exists");
				return ;
			}else{
			
				System.out.print("Overwrite existing file " + toFile.getName() + " file size diff=" + diff 
					+ "? (Y/N): ");
				System.out.flush();
				BufferedReader in = new BufferedReader(new InputStreamReader(
						System.in));
				String response = in.readLine();
				if (!response.equals("Y") && !response.equals("y")){
					System.out.println("Skipping FILE " + toFileName + " As it exists");
					return ;
				}
			}	
			

		} else {
			String parent = toFile.getParent();
			if (parent == null)
				parent = System.getProperty("user.dir");
			File dir = new File(parent);
			if (!dir.exists())
				throw new IOException("FileCopy: "
						+ "destination directory doesn't exist: " + parent);
			if (dir.isFile())
				throw new IOException("FileCopy: "
						+ "destination is not a directory: " + parent);
			if (!dir.canWrite())
				throw new IOException("FileCopy: "
						+ "destination directory is unwriteable: " + parent);
		}

		FileInputStream from = null;
		FileOutputStream to = null;
		try {
			from = new FileInputStream(fromFile);
			to = new FileOutputStream(toFile);
			byte[] buffer = new byte[4096];
			int bytesRead;

			while ((bytesRead = from.read(buffer)) != -1)
				to.write(buffer, 0, bytesRead); // write
		} finally {
			if (from != null)
				try {
					from.close();
				} catch (IOException e) {
					;
				}
			if (to != null)
				try {
					to.close();
				} catch (IOException e) {
					;
				}
		}

	}
	

	/**
	 * Method Not support in AppEngine
	 * @param fromFile
	 * @param toFile
	 * @throws IOException
	 */
	public static void copy(File fromFile, File toFile) throws IOException {
		copy(fromFile, toFile , false);
	}
}
